package com.qire.manhua.model.entity;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;


import com.qire.manhua.view.comicReader.renderer.BulletScreen;
import com.qire.manhua.view.comicReader.renderer.PictureResource;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 * 弹幕实例
 */
public class BulletScreenInfo {

    public static final String WhiteColorTag   = "white";
    public static final String YellowColorTag  = "yellow";
    public static final String GreenColorTag   = "green";
    public static final String RedColorTag     = "red";
    public static final String BlueColorTag    = "blue";
    public static final String PinkColorTag    = "pink";

    private static final int[] fontSizes = {16, 20, 24};
    private static final HashMap<String, Integer[]> colorMap = new HashMap<>();
    static {
        colorMap.put(WhiteColorTag, new Integer[]{Color.rgb(0xff, 0xff, 0xff), Color.rgb(0x33, 0x33, 0x33)});
        colorMap.put(YellowColorTag,new Integer[]{Color.rgb(0xff, 0xf9, 0x48), Color.rgb(0x58, 0x51, 0x00)});
        colorMap.put(GreenColorTag, new Integer[]{Color.rgb(0xb7, 0xff, 0x45), Color.rgb(0x1c, 0x41, 0x00)});
        colorMap.put(RedColorTag,   new Integer[]{Color.rgb(0xff, 0x4f, 0x4f), Color.rgb(0x3f, 0x00, 0x00)});
        colorMap.put(BlueColorTag,  new Integer[]{Color.rgb(0x55, 0xdd, 0xff), Color.rgb(0x00, 0x38, 0x46)});
        colorMap.put(PinkColorTag,  new Integer[]{Color.rgb(0xff, 0x5d, 0xde), Color.rgb(0x45, 0x00, 0x37)});
    }

    public final int    id;
    public final String content;
    public final int    fontSizeNumber;
    public final String fontColorTag;

    private BigDecimal xRatio;
    private BigDecimal yRatio;
    private String contentPictureToken;       //章节图片Token
    private String contentPictureUrl;
    private int        imgPx;
    private PictureResource pictureResource;

    public BulletScreenInfo(String content, int fontSizeNumber, String fontColorTag) {
        this.id              = 0;
        this.content         = content;
        this.fontSizeNumber  = fontSizeNumber;
        this.fontColorTag    = fontColorTag;
    }

    public BulletScreenInfo(int id, String content, int fontSizeNumber, String fontColorTag, BigDecimal xRatio, BigDecimal yRatio, String contentPictureToken, String contentPictureUrl, int imgPx) {

        this.id                  = id;
        this.xRatio              = xRatio;
        this.yRatio              = yRatio;
        this.content             = content;
        this.fontSizeNumber      = fontSizeNumber;
        this.fontColorTag        = fontColorTag;
        this.contentPictureToken = contentPictureToken;
        this.contentPictureUrl   = contentPictureUrl;
        this.imgPx               = imgPx;

    }

    public BigDecimal xRatio() {
        return xRatio;
    }
    public BigDecimal yRatio() {
        return yRatio;
    }

    public String contentPictureToken() {
        return this.contentPictureToken;
    }

    public Integer[] fontColor() {
        Integer[] colors = colorMap.get(fontColorTag);
        if(colors == null) {
            return colorMap.get("white");
        }
        return colors;
    }

    public int fontSize() {
        return fontSizes[fontSizeNumber-1];
    }

    /**计算坐标*/
    private final int calcXY(BigDecimal xyRatio, int sideLength) {
        return xyRatio.multiply(new BigDecimal(sideLength)).intValue();
    }

    public void loadPictureResource(PictureResource pictureResource, Point location) {

//        Rect pictureRect = pictureResource.getBitmapRect();
        Rect pictureRect = pictureResource.calcViewportRect();

        BigDecimal bigDecimalX = new BigDecimal(location.x);
        BigDecimal bigDecimalY = new BigDecimal(location.y);
        BigDecimal bigDecimalW = new BigDecimal(pictureRect.width());
        BigDecimal bigDecimalH = new BigDecimal(pictureRect.height());

        this.xRatio = bigDecimalX.divide(bigDecimalW, 8, BigDecimal.ROUND_HALF_UP);
        this.yRatio = bigDecimalY.divide(bigDecimalH, 8, BigDecimal.ROUND_HALF_UP);
        this.pictureResource = pictureResource;
        this.contentPictureToken = pictureResource.token;
    }

    public boolean isEmpty() {
        return pictureResource == null;
    }

    public BulletScreen toBulletScreen(PictureResource pictureResource) {
        this.pictureResource = pictureResource;
        this.contentPictureToken = pictureResource.token;
        return toBulletScreen();
    }
    public BulletScreen toBulletScreen() {
//        Rect pictureRect = pictureResource.getBitmapRect();
        Rect pictureRect = pictureResource.calcViewportRect();
        int x   = calcXY(xRatio, pictureRect.width());
        int y   = calcXY(yRatio, pictureRect.height());
        Integer[] fontColors = fontColor();
        return builder(pictureResource, new Point(x,y), content,fontColors[0], fontColors[1], fontSize());
    }

    public static BulletScreen builder(PictureResource pictureResource, Point location, String content, int fontColor, int shadowColor, int fontSize) {
        return new BulletScreen(pictureResource, location, content, fontColor, shadowColor, fontSize);
    }

    public static Integer[] queryFontColorByTag(String fontColorTag) {
        return colorMap.get(fontColorTag);
    }

}
